#include <stdio.h>
#include <stdbool.h>

void Swap(int* p1, int* p2) {
    int tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}

int* sortArrayByParityII(int* nums, int numsSize, int* returnSize) {
    if(numsSize < 2)
        return nums;
    *returnSize = numsSize;
    int ptr0 = 0;
    int ptr1 = 1;

    while(ptr0 < numsSize - 1 ){
        while(ptr0 < numsSize - 1){
            if(nums[ptr0] % 2 == 1)
                break;
            ptr0 += 2;
        }
        while(ptr1 < numsSize){
            if(nums[ptr1] % 2 == 0)
                break;
            ptr1 += 2;
        }

        if(ptr0 < numsSize - 1) {
            // int tmp = nums[ptr0];
            // nums[ptr0] = nums[ptr1];
            // nums[ptr1] = tmp;

            Swap(&nums[ptr0], &nums[ptr1]);
            //Swap(nums + ptr0, nums + ptr1);

            ptr0 += 2;
            ptr1 += 2;
        }
    }

    return nums;
}

int main() {
    int nums[] = {4,2,8,9,5,7};
    int numsSize = sizeof(nums) / sizeof(int);
    int size;
    int * rs = sortArrayByParityII(nums, numsSize, &size);

    for(int i = 0; i < size; i++)
        printf(" %d ", rs[i]);
    return 0;
}